package com.huang.auth.converter;

import com.huang.auth.entity.po.AuthResourcePO;
import com.huang.auth.entity.po.AuthRolePO;
import com.huang.auth.entity.po.AuthUserPO;
import com.huang.auth.entity.vo.AuthUserInfoVO;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author HuangShen
 * @Description AuthUserInfo
 * @create 2021-11-06 14:34
 */
public class AuthUserInfoVOConverter {


    public static AuthUserInfoVO builder(AuthUserPO authUserPO, List<AuthRolePO> authRolePOS, List<AuthResourcePO> authResourcePOS) {
        AuthUserInfoVO authUserInfoVO = AuthUserInfoVO.builder()
                .account(authUserPO.getAccount())
                .avatar(authUserPO.getAvatar())
                .email(authUserPO.getEmail())
                .mobile(authUserPO.getMobile())
                .name(authUserPO.getName())
                .sex(authUserPO.getSex())
                .username(authUserPO.getAccount())
                .workDescribe(authUserPO.getWorkDescribe()).build();
        if (Objects.nonNull(authResourcePOS)) {
            Set<String> permissions = authResourcePOS.stream().map(AuthResourcePO::getCode).collect(Collectors.toSet());
            authUserInfoVO.setPermissions(permissions);
        }

        if (Objects.nonNull(authRolePOS)) {
            Set<String> roles = authRolePOS.stream().map(AuthRolePO::getCode).collect(Collectors.toSet());
            authUserInfoVO.setRoles(roles);
        }

        return authUserInfoVO;
    }
}
